package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.apis.admin.AdChannelControllerApi;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.validator.HeimaValidatorAdd;
import com.heima.model.common.validator.HeimaValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理", tags = "频道管理-AdChannel", description = "频道管理API")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    AdChannelService adChannelService;

    /**
     * 修改&状态
     *
     * @param adChannel
     * @return
     */
    @PutMapping("update")
    @Override
    public ResponseResult update(@RequestBody @Validated({HeimaValidatorUpdate.class}) AdChannel adChannel) {
        return adChannelService.update(adChannel);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    @Override
    public ResponseResult deleteById(@PathVariable("id") Integer id) {
        return adChannelService.deleteById(id);
    }

    /**
     * 新增
     *
     * @param adChannel
     * @return
     */
    @PostMapping("save")
    @Override
    public ResponseResult insert(@RequestBody @Validated({HeimaValidatorAdd.class}) AdChannel adChannel) {
        return adChannelService.insert(adChannel);
    }

    /**
     * 条件查询频道列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @Override
    @ApiOperation(value = "条件分页查询频道列表")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto) {
        return adChannelService.findByNameAndPage(dto);
    }


    @GetMapping("/channels")
    @Override
    public ResponseResult findAll() {
        List<AdChannel> channels = adChannelService.list();
        return ResponseResult.okResult(channels);
    }
}
